<%
Class FastAsp_Language
    Private m_Lang       '当前语言
    Private m_LangPath   '语言包目录
    Private m_LangData   '语言数据
    
    Private Sub Class_Initialize()
        m_Lang = GetConfig("DefaultLang")
        m_LangPath = GetConfig("LangPath")
        If m_LangPath = "" Then m_LangPath = "/languages"
        Set m_LangData = Server.CreateObject("Scripting.Dictionary")
        LoadLanguage()
    End Sub
    
    '设置语言
    Public Function SetLang(byval lang)
        m_Lang = lang
        LoadLanguage()
        Set SetLang = Me
    End Function
    
    '获取语言
    Public Function GetLang()
        GetLang = m_Lang
    End Function
    
    '获取翻译
    Public Function [Get](byval key)
        If m_LangData.Exists(key) Then
            [Get] = m_LangData(key)
        Else
            [Get] = key
        End If
    End Function
    
    '获取带参数替换的翻译
    Public Function Gets(byval key, byval args)
        Dim text : text = [Get](key)
        If Not IsArray(args) Then Exit Function
        
        Dim i
        For i = 0 To UBound(args)
            text = Replace(text, "{" & i & "}", args(i))
        Next
        Gets = text
    End Function
    
    '加载语言包
    Private Sub LoadLanguage()
        m_LangData.RemoveAll
        
        Dim langFile : langFile = Server.MapPath(m_LangPath & "/" & m_Lang & ".asp")
        If Not LoadFromFile(langFile) Then
            '如果找不到指定语言，加载默认语言
            langFile = Server.MapPath(m_LangPath & "/" & GetConfig("DefaultLang") & ".asp")
            LoadFromFile langFile
        End If
    End Sub
    
    '从文件加载语言数据
    Private Function LoadFromFile(byval file)
        Dim fso : Set fso = Server.CreateObject("Scripting.FileSystemObject")
        If Not fso.FileExists(file) Then
            LoadFromFile = False
            Set fso = Nothing
            Exit Function
        End If
        
        Dim stream : Set stream = fso.OpenTextFile(file, 1, False)
        Dim content : content = stream.ReadAll()
        stream.Close
        Set stream = Nothing
        Set fso = Nothing
        
        If content = "" Then
            LoadFromFile = False
            Exit Function
        End If
        
        '解析语言数据
        Dim lines : lines = Split(content, vbCrLf)
        Dim line, pos, key, value
        For Each line In lines
            line = Trim(line)
            If line <> "" And Left(line, 1) <> "'" Then
                pos = InStr(line, "=")
                If pos > 0 Then
                    key = Trim(Left(line, pos - 1))
                    value = Trim(Mid(line, pos + 1))
                    If key <> "" Then m_LangData.Add key, value
                End If
            End If
        Next
        
        LoadFromFile = True
    End Function
    
    Private Sub Class_Terminate()
        Set m_LangData = Nothing
    End Sub
End Class
%> 